﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using TotalCrytography.Protector;
using TotalCrytography.Data;

namespace TotalCrytography
{
    public partial class RegisterAccountGUI : Form
    {
        private UserDTO m_user;
        public UserDTO User
        {
            get { return m_user; }
            set { m_user = value; }
        }

        private string m_pass;

        public string Password
        {
            get { return m_pass; }
            set { m_pass = value; }
        }

        public RegisterAccountGUI()
        {
            InitializeComponent();
            InitKeySize();                
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void InitKeySize()
        {
            List<int> lstKeySize = new List<int>();
            for (int i = 512; i <= 1024; i+=64)
            {
                lstKeySize.Add(i);
            }
            cbKeySize.DataSource = lstKeySize;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            tbEmail.Text = string.Empty;
            tbReEmail.Text = string.Empty;
            tbPass.Text = string.Empty;
            tbRePass.Text = string.Empty;
            tbName.Text = string.Empty;
            dtpBirthday.Value = new DateTime(1980, 1, 1);
            tbPhone.Text = string.Empty;
            tbAddress.Text = string.Empty;
            cbKeySize.Text = "512";
        }

        private void tbReEmail_Leave(object sender, EventArgs e)
        {
            if (tbEmail.Text != tbReEmail.Text)
            {
                lbReEmailErr.Visible = true;
            }
            else
            {
                lbReEmailErr.Visible = false;
            }
        }


        private void tbEmail_Leave(object sender, EventArgs e)
        {
            // check email
            string matchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                                + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                                + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
            Regex emailRegex = new Regex(matchEmailPattern);
            if (!emailRegex.IsMatch(tbEmail.Text))
            {
                lbEmailErr.Visible = true;
            }
            else
            {
                lbEmailErr.Visible = false;
            }
        }

        private void tbPass_Leave(object sender, EventArgs e)
        {
            if (tbPass.TextLength == 0)
            {
                lbPassErr.Visible = true;
            }
            else
            {
                lbPassErr.Visible = false;
            }
        }

        private void tbRePass_Leave(object sender, EventArgs e)
        {
            if (tbPass.Text != tbRePass.Text)
            {
                lbRePassErr.Visible = true;
            }
            else
            {
                lbRePassErr.Visible = false;
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (!lbEmailErr.Visible && !lbPassErr.Visible && !lbReEmailErr.Visible && !lbRePassErr.Visible)
            {
                UserDTO userDto = new UserDTO();
                userDto.Email = tbEmail.Text;
                userDto.Salt = Hash_Algorithm.CreateSalt(4, 8);
                userDto.HashPass = Hash_Algorithm.CreateHashWithSalt(tbPass.Text, "SHA1", userDto.Salt);
                userDto.Name = tbName.Text;
                userDto.Birthday = dtpBirthday.Value.ToShortDateString();
                userDto.Phone = tbPhone.Text;
                userDto.Address = tbAddress.Text;
                userDto.Keysize = int.Parse(cbKeySize.Text);
                // generate pairs of key
                string[] m_key = RSA_Algorithm.generateKey(userDto.Keysize);
                string privateKey = m_key[0];
                userDto.PublicKey = m_key[1];
                // encrypt private key
                Symmetric_Provider symProvider = new Symmetric_Provider(tbPass.Text);
                userDto.PrivateKey = symProvider.EncryptString(privateKey);
                //insert here
                if (UserBUS.Insert(userDto))
                {
                    MainGUI.SetCurrentUser(userDto);
                    MainGUI.SetCurrentPass(tbPass.Text);
                    m_user = userDto;
                    m_pass = tbPass.Text;
                    MessageBox.Show("Register successfully!", "TotalCrytography", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
                else
                    MessageBox.Show("User email has existed!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            else
                MessageBox.Show("Input data is not valid!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }
    }
}
